package com.example.demo.service.impl;

import com.example.demo.dao.MemberRepository;
import com.example.demo.dto.MemberDTO;
import com.example.demo.model.Member;
import com.example.demo.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;

/**
 * @description: 会员业务
 */
@Slf4j
@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberRepository memberRepository;

    @Override
    public MemberDTO detailMember() {
        MemberDTO vo = new MemberDTO();
        Member member = findById(4L);
        BeanUtils.copyProperties(member, vo);
        return vo;
    }

    @Override
    public void guideMember() {
        Member member = findById(4L);
        memberRepository.updateMemberGuide(member.getId());
    }

    private Member findById(Long id) {
        Optional<Member> member = memberRepository.findById(id);
        return member.isPresent() ? member.get() : null;
    }
}
